﻿using System;
using System.Collections.Generic;

namespace ListSortMethod
{
    public class Program
    {
        public static void Main()
        {
            // Create a list of parts.
            List<Part> parts = new List<Part>();

            // Add parts to the list.
            parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
            parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });
            parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ;
            // Name intentionally left null.
            parts.Add(new Part() { PartId = 1334 });
            parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
            parts.Add(new Part() { PartName = "cassette", PartId = 1534 });

            // Write out the parts in the list. This will call the overridden
            // ToString method in the Part class.
            Console.WriteLine("\nBefore sort:");
            foreach (Part aPart in parts)
            {
                Console.WriteLine(aPart);
            }

            // Call Sort on the list. This will use the
            // default comparer, which is the Compare method
            // implemented on Part.
            parts.Sort();

            Console.WriteLine("\nAfter sort by part number:");
            foreach (Part aPart in parts)
            {
                Console.WriteLine(aPart);
            }

            // This shows calling the Sort(Comparison(T) overload using
            // an anonymous method for the Comparison delegate.
            // This method treats null as the lesser of two values.
            parts.Sort(delegate (Part x, Part y)
            {
                if (x.PartName == null && y.PartName == null) return 0;
                else if (x.PartName == null) return -1;
                else if (y.PartName == null) return 1;
                else return x.PartName.CompareTo(y.PartName);
            });

            Console.WriteLine("\nAfter sort by name:");
            foreach (Part aPart in parts)
            {
                Console.WriteLine(aPart);
            }

            /*

                Before sort:
            ID: 1434   Name: regular seat
            ID: 1234   Name: crank arm
            ID: 1634   Name: shift lever
            ID: 1334   Name:
            ID: 1444   Name: banana seat
            ID: 1534   Name: cassette

            After sort by part number:
            ID: 1234   Name: crank arm
            ID: 1334   Name:
            ID: 1434   Name: regular seat
            ID: 1444   Name: banana seat
            ID: 1534   Name: cassette
            ID: 1634   Name: shift lever

            After sort by name:
            ID: 1334   Name:
            ID: 1444   Name: banana seat
            ID: 1534   Name: cassette
            ID: 1234   Name: crank arm
            ID: 1434   Name: regular seat
            ID: 1634   Name: shift lever

            */
        }
    }
}
